package com.app.mingluxing.my;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.app.mingluxing.R;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.bean.ResultBaseBean;
import com.app.mingluxing.net.HttpUtil;
import com.app.mingluxing.ui.LoginActivity;
import com.app.mingluxing.utils.CommonUtils;
import com.app.mingluxing.utils.Constant;

/**
 * 修改密码页面
 * Created by Administrator on 2015/8/12.
 */
public class ModifyCodeActivity extends BaseActivity {

    private EditText et_old_pwd;
    private EditText et_new_pwd;
    private ImageView iv_delete_old;
    private ImageView iv_delete_new;
    private EditText et_new_again;
    private ImageView iv_delete_again;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_modify_code);

        init();

    }

    private void init() {

        ImageView tv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);

        tv_left.setOnClickListener(this);
        tv_title.setText(getResources().getString(R.string.modify_code));
        tv_right.setText(getResources().getString(R.string.done));

        et_old_pwd = (EditText) findViewById(R.id.et_old_pwd);
        et_new_pwd = (EditText) findViewById(R.id.et_new_pwd);
        et_new_again = (EditText) findViewById(R.id.et_new_again);

        iv_delete_old = (ImageView) findViewById(R.id.iv_delete_old);
        iv_delete_new = (ImageView) findViewById(R.id.iv_delete_new);
        iv_delete_again = (ImageView) findViewById(R.id.iv_delete_again);


        iv_delete_old.setOnClickListener(this);
        iv_delete_new.setOnClickListener(this);
        iv_delete_again.setOnClickListener(this);
        tv_right.setOnClickListener(this);

        initDelete();

    }

    private void initDelete() {

        et_old_pwd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    //显示叉号
                    iv_delete_old.setVisibility(et_old_pwd.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);

                    et_old_pwd.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                            //显示叉号
                            iv_delete_old.setVisibility(et_old_pwd.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);

                        }

                        @Override
                        public void afterTextChanged(Editable editable) {
                        }
                    });

                } else {
                    iv_delete_old.setVisibility(View.INVISIBLE);

                }
            }
        });


        et_new_pwd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    //显示叉号
                    iv_delete_new.setVisibility(et_new_pwd.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);

                    et_new_pwd.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                            //显示叉号
                            iv_delete_new.setVisibility(et_new_pwd.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);

                        }

                        @Override
                        public void afterTextChanged(Editable editable) {
                        }
                    });

                } else {
                    iv_delete_new.setVisibility(View.INVISIBLE);

                }
            }
        });

        et_new_again.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    //显示叉号
                    iv_delete_again.setVisibility(et_new_again.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);

                    et_new_again.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                            //显示叉号
                            iv_delete_again.setVisibility(et_new_again.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);

                        }

                        @Override
                        public void afterTextChanged(Editable editable) {
                        }
                    });

                } else {
                    iv_delete_again.setVisibility(View.INVISIBLE);

                }
            }
        });
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

            case R.id.iv_delete_old:

                et_old_pwd.setText("");

                break;

            case R.id.iv_delete_new:

                et_new_pwd.setText("");

                break;

            case R.id.iv_delete_again:

                et_new_again.setText("");

                break;

            case R.id.tv_right://完成

                if (CommonUtils.isNull(et_old_pwd.getText().toString().trim())) {
                    showToast("请输入旧密码");
                    return;
                }

                if (et_old_pwd.getText().toString().trim().length() < 6) {
                    showToast("密码不正确");
                    return;
                }

                if (CommonUtils.isNull(et_new_pwd.getText().toString().trim())) {
                    showToast("请输入新密码");
                    return;
                }

                if (et_new_pwd.getText().toString().trim().length() < 6) {
                    showToast("密码长度在6-16位之间");
                    return;
                }

                if (!et_new_pwd.getText().toString().trim().equals(et_new_again.getText().toString().trim())) {
                    showToast("您两次输入的密码不一致");
                    return;
                }

                modifyCode();

                break;

        }
    }

    /**
     * 修改密码
     */
    private void modifyCode() {


        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("oldpwd", CommonUtils.toMd5(et_old_pwd.getText().toString().trim().getBytes()).toUpperCase());
        j.put("newpwd", CommonUtils.toMd5(et_new_pwd.getText().toString().trim().getBytes()).toUpperCase());

        HttpUtil.setPostRequest("修改中...", this, Constant.WEB_ROOT + Constant.MODUFYCODE,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            ResultBaseBean bean = JSONObject.parseObject(str, ResultBaseBean.class);

                            if (bean.code == Constant.STATUS_SUCCESS) {

                                showToast("修改成功");

                                Intent mIntent = new Intent(ModifyCodeActivity.this, LoginActivity.class);
                                startActivity(mIntent);

                            } else {
                                showToast(Constant.showErrorMsg(bean.code, ModifyCodeActivity.this));
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }, null);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(getClass().getName());
    }
}
